package bravo.xc.com.xc_bravo_android.operation.withdraw.withdrawmain;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.adapter.spinnerAdapter;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.CommonBeans;
import bravo.xc.com.xc_bravo_android.beans.GetBankResultBeans;
import bravo.xc.com.xc_bravo_android.beans.SendCodeBeans;
import bravo.xc.com.xc_bravo_android.beans.SendMoneyCodeBeans;
import bravo.xc.com.xc_bravo_android.beans.WithDrawBeans;
import bravo.xc.com.xc_bravo_android.beans.WithDrawResultBeans;
import bravo.xc.com.xc_bravo_android.operation.withdraw.withdrawrecord.WithdrawRecordActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class WithdrawActivity extends BaseMVPActivity<WithdrawContract.WithdrawView,WithdrawContract.WithdrawPresenter> implements WithdrawContract.WithdrawView,View.OnClickListener {

    private LinearLayout layout_title_left;
    private TextView tv_title_meg;
    private TextView withDrawMoney,phoneNum,sendCheck;
    private EditText withDrawSelf,code;
    private Button sendWithDraw;
    private Spinner spinner;
    private LinearLayout linearLayout;
    private spinnerAdapter spinnerAdapter;
    private List<String> adapter,bankId;
    private int selectPosition;
    private SendMoneyCodeBeans sendMoneyCodeBeans;
    private WithDrawBeans withDrawBeans;
    private WithDrawResultBeans withDrawResultBeans;

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.layout_withdraw_record:
                Intent intent = new Intent(WithdrawActivity.this, WithdrawRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_withdraw_confirm:
                if(checkNull()){
                    withDrawBeans.setBankId(String.valueOf(bankId.get(selectPosition)));
                    withDrawBeans.setCode(code.getText().toString().trim());
                    withDrawBeans.setNum(withDrawSelf.getText().toString().trim());
                    withDrawBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
                    withDrawBeans.setPhoneNum(phoneNum.getText().toString().trim());
                    presenter.doWithDraw(withDrawBeans);
                }

                break;
            case R.id.tv_withdraw_sendValidationCode:
                if(!withDrawSelf.getText().toString().trim().isEmpty()){

                    if (phoneNum.getText().toString().trim().isEmpty()){
                        Toast.makeText(this,"手机号获取失败，请重试",Toast.LENGTH_SHORT).show();
                    }else {

                        sendMoneyCodeBeans = new SendMoneyCodeBeans();
                        sendMoneyCodeBeans.setMoney(withDrawSelf.getText().toString().trim());
                        sendMoneyCodeBeans.setBankNum(withDrawResultBeans.getData().getBanks().get(selectPosition).getBankNum());
                        sendMoneyCodeBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
                        sendMoneyCodeBeans.setPhoneNum(phoneNum.getText().toString().trim());
                        presenter.getCodes(sendMoneyCodeBeans);
                    }
                }else{
                    Toast.makeText(this,"请输入提现金额",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.layout_title_left:
                finish();
                break;
        }
    }

    @Override
    public void toNewActivity() {
        Intent intent = new Intent(this,WithdrawRecordActivity.class);
        startActivity(intent);
        finish();
    }

    private boolean checkNull(){
        if(code.getText().toString().trim().isEmpty()){
            Toast.makeText(this,"请输入验证码",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(withDrawSelf.getText().toString().trim().isEmpty()){
            Toast.makeText(this,"请输入提现金额",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(phoneNum.getText().toString().trim().isEmpty()){
            Toast.makeText(this,"手机号获取失败，请稍后重试",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;

    }
    @Override
    public void showToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected WithdrawContract.WithdrawPresenter createPresnter() {
        return new WithdrawPresenter();
    }

    @Override
    protected void initView() {
        layout_title_left=(LinearLayout)findViewById(R.id.layout_title_left);
        tv_title_meg=(TextView)findViewById(R.id.tv_title_msg);
        tv_title_meg.setText("提现中心");
        sendCheck = (TextView) findViewById(R.id.tv_withdraw_sendValidationCode);
        withDrawMoney = (TextView) findViewById(R.id.tv_withdraw_money);
        sendWithDraw = (Button) findViewById(R.id.btn_withdraw_confirm);
        withDrawSelf = (EditText) findViewById(R.id.et_withdraw_input_money);
        phoneNum = (TextView) findViewById(R.id.tv_withdraw_phoneNum);
        linearLayout = (LinearLayout) findViewById(R.id.layout_withdraw_record);
        spinner= (Spinner)findViewById(R.id.sp_withdraw_choose_bankcard) ;
        code =(EditText)findViewById(R.id.et_withdraw_input_validationCode);
        layout_title_left.setOnClickListener(this);
        sendCheck.setOnClickListener(this);
        linearLayout.setOnClickListener(this);
        sendWithDraw.setOnClickListener(this);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectPosition = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(WithdrawActivity.this,"你什么都未选中",Toast.LENGTH_SHORT).show();
            }

        });

    }

    @Override
    protected void initData() {
        withDrawResultBeans = new WithDrawResultBeans();
        adapter = new ArrayList<String>();
        bankId = new ArrayList<String>();
        withDrawBeans = new WithDrawBeans();
        CommonBeans commonBeans = new CommonBeans();
        commonBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
        presenter.doGetInit(commonBeans);

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(WithdrawActivity.this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    @Override
    public void setAdapter(WithDrawResultBeans withDrawResultBeans) {
        this.withDrawResultBeans = withDrawResultBeans;
        for(int i=0;i<withDrawResultBeans.getData().getBanks().size();i++){
            adapter.add(withDrawResultBeans.getData().getBanks().get(i).getBankName());
            bankId.add(String.valueOf(withDrawResultBeans.getData().getBanks().get(i).getId()));
        }
        spinnerAdapter = new spinnerAdapter(this,R.layout.item_spinner,adapter);
        spinnerAdapter.setDropDownViewResource(R.layout.item_spinner);//设置下拉样式
        spinner.setAdapter(spinnerAdapter);
        //spinner.setSelection(adapter.size() - 1, true);//显示List最后一项作为Hint
    }


    @Override
    public void setPhone(String phone) {
        phoneNum.setText(phone);
    }

    @Override
    public void setNum(float num) {
        withDrawMoney.setText(String.valueOf(num)+"元");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


}
